package services;

import dao.UnloadPlace_Repository;
import models.*;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;


/**
 * Provide JPA operations running inside of a thread pool sized to the connection pool
 */
public class UnloadPlace_Repository_JPA implements UnloadPlace_Repository {

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public UnloadPlace_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    @SuppressWarnings("unchecked")
    @Override
    public CompletionStage<List<UnloadPlace_F_Entity>> GetUnloadPlaceByDeptID(String DeptID)
    {
    	return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
            	// 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
            	String strSql = "select * from dbo.f_TabUnloadPlace('" + DeptID+ "') as UnloadPlace";
            	return entityManager.createNativeQuery(strSql, UnloadPlace_F_Entity.class).getResultList();
            });
        }, executionContext);    	
    }

    @SuppressWarnings("unchecked")
    @Override
    public CompletionStage<List<UserRoute_C_Entity>> ConfirmStatus()
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "select * from t_UserRoute order by applicationId desc";
                return entityManager.createNativeQuery(strSql, UserRoute_C_Entity.class).getResultList();
            });
        }, executionContext);
    }

    @SuppressWarnings("unchecked")
    @Override
    public CompletionStage<List<UnloadPlace_F_Entity>> UserConfirmUnloadPlace(String ApplyID, String UnloadPlace)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "exec p_UserConfirmUnloadPlace @ApplyID = '"+ApplyID+"',@UnloadPlace = '"+UnloadPlace+"'";
                return entityManager.createNativeQuery(strSql, UnloadPlace_F_Entity.class).getResultList();
            });
        }, executionContext);
    }

    @Override
    public CompletionStage<XMLString_Entity> getGPSUnloadPlace(String PhoneNumber, Double Lat, Double Lng) {
            return supplyAsync(() -> {
                return jpaApi.withTransaction(entityManager -> {
                    // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                    String strSql = "EXEC p_GetGPSUnloadPlace @phonenumber = '"+PhoneNumber+"' ,@Lat = '"+Lat+"', @Lng = '"+Lng+"'";
                    return (XMLString_Entity)(entityManager.createNativeQuery(strSql, XMLString_Entity.class).getSingleResult());
                });
            }, executionContext);
    }

    @Override
    public CompletionStage<List<UnLoadPlace_T_Entity>> saveCoordinates(String PhoneNumber, String UnloadPlaceName, Double Lat, Double Lng, BigDecimal mileage) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "EXEC p_SaveCoordinates '"+PhoneNumber+"','"+UnloadPlaceName+"','"+Lat+"','"+Lng+"','"+mileage+"'";
                return entityManager.createNativeQuery(strSql, UnLoadPlace_T_Entity.class).getResultList();
            });
        }, executionContext);
    }

    @Override
    public CompletionStage<List<UnLoadPlace_T_Entity>> getUnloadPlaceByApplyId(Integer applyId) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "SELECT * FROM f_getUnloadPlaceByApplyId("+applyId+")";
                return entityManager.createNativeQuery(strSql, UnLoadPlace_T_Entity.class).getResultList();
            });
        }, executionContext);
    }

    @Override
    public CompletionStage<List<UnLoadPlace_T_Entity>> getUnloadPlaceByPoundNo(String poundNo) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "SELECT * FROM dbo.f_getUnloadPlaceByPoundNo("+poundNo+")";
                return entityManager.createNativeQuery(strSql, UnLoadPlace_T_Entity.class).getResultList();
            });
        }, executionContext);
    }


//    @Override
//    public CompletionStage<XMLString_Entity> saveCoordinates(String PhoneNumber, String UnloadPlaceName, Double Lat, Double Lng, BigDecimal mileage) {
//        return supplyAsync(() -> {
//            return jpaApi.withTransaction(entityManager -> {
//                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
//                String strSql = "EXEC p_retSaveCoordinates '"+PhoneNumber+"','"+UnloadPlaceName+"','"+Lat+"','"+Lng+"','"+mileage+"'";
//                return (XMLString_Entity)(entityManager.createNativeQuery(strSql, XMLString_Entity.class).getSingleResult());
//            });
//        }, executionContext);
//    }
}
